package deprecated.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import domain.dao.impl.DAOContactGroup;
import domain.dao.interfaces.IDAOUser;
import domain.entities.impl.User;
import util.DomainUtils;
import util.FormArgumentsException;

/**
 * Servlet implementation class AddUserServlet
 */
public class AddUserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static int MINIMAL_LOGIN_LENGHT = 3;
	private static int MINIMAL_PASSWORD_LENGHT = 6;



	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddUserServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String login = DomainUtils.trim(request.getParameter("login"));
		String password = request.getParameter("password");
		String passwordConfirmation = request.getParameter("password_confirmation");
		
		ApplicationContext context = util.SpringUtils.getApplicationContext();

		try {
			checks(request, login, password, passwordConfirmation);
			IDAOUser daoUser = (IDAOUser) context.getBean("beanDAOUser");
			daoUser.addUser( login, password);
			
//			DAOFactory.getDaoUser().addUser(null, login, password);

			DomainUtils.displayInfoMessage(
					request, 
					DomainUtils.REGISTER_ALERT_CURRENT_COLOR, 
					"Thank you for register. You can now connect yourself from the home page with the login \""+login+"\".");
		}
		catch(Exception e) {

			if((e instanceof FormArgumentsException) == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "An error occur on the server.");
			}

			DomainUtils.keepParametersContent(request);
		}


		request.getRequestDispatcher("pages/register.jsp").forward(request, response);
	}

	private void checks(
			HttpServletRequest request, String login, 
			String password, String passwordConfirmation) throws Exception {

		HttpSession session = request.getSession(false);
		ApplicationContext context = util.SpringUtils.getApplicationContext();

		//Checking session
		if(session == null) {
			throw new Exception();
		}

		if(session.getAttribute(DomainUtils.CURRENT_USER_STRING) != null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "You cannot realize this action because you are connected.");
			throw new FormArgumentsException();
		}


		//Checking login
		if(login == null || DomainUtils.startByALetter(login) == false){
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "The login must start by a letter.");
			throw new FormArgumentsException();
		}

		if(DomainUtils.match(login, "\\w+") == false) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "The login must contains only letter or digital character.");
			throw new FormArgumentsException();
		}

		IDAOUser daoUser = (IDAOUser) context.getBean("beanDAOUser");
		//DAOFactory.getDaoUser().getByLogin(login) != null
		if(daoUser.getByLogin(login) != null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "This login is already used.");
			throw new FormArgumentsException();
		}

		if(login.length() < MINIMAL_LOGIN_LENGHT){
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "The login must have at least "+MINIMAL_LOGIN_LENGHT+" characters.");
			throw new FormArgumentsException();
		}


		//Checking password
		if(password == null || password.length() < MINIMAL_PASSWORD_LENGHT){
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "The password must have at least "+MINIMAL_PASSWORD_LENGHT+" characters.");
			throw new FormArgumentsException();
		}

		if(password.equals(passwordConfirmation) == false) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "The password confirmation must match the password.");
			throw new FormArgumentsException();
		}
	}
}
